import axios from 'axios'

const request = (url, params = {}, data = {}, method = "get") => {
    return new Promise((reslove, reject) => {
        axios({
            method,
            url,
            params,
            data
        }).then(res => {
            reslove(res);
        }).catch(res => {
            reject(res);
        })
    });
}

//封装get请求
const get = (url, params) => {
    return request(url, params, {}, "get")
}

//封装post请求
const post = (url, data) => {
    return request(url, {}, data, "post");
}

// 添加请求拦截器
axios.interceptors.request.use(function (config) {
    //设置请求头
    config.headers.Authorization = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImp0aSI6IjRmMWcyM2ExMmFhIn0.eyJpc3MiOiJhcGkuMTkxMi5jb20iLCJhdWQiOiJhcGlfMTkxMl9hcHAiLCJqdGkiOiI0ZjFnMjNhMTJhYSIsImlhdCI6MTc1NjE5MjAwOSwibmJmIjoxNzU2MTkyMDA5LCJleHAiOjE3NTg3ODQwMDksInVpZCI6MTQzMDZ9.AZVue_4PoqhnFdn7ReDqdpnuEWXzzisTgTSopwdHz2Q';
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    // 2xx 范围内的状态码都会触发该函数。
    // 对响应数据做点什么
    return response;
}, function (error) {
    // 超出 2xx 范围的状态码都会触发该函数。
    // 对响应错误做点什么
    return Promise.reject(error);
});

export default { request, get, post }